'use strict';

const request = require('../request').request,
    md5 = require('../request').md5,
    upload = require('../request').upload,
    superagent = require('superagent'),
    expect = require('chai').expect;

const fs = require('fs');

function login(cb) {
    request({ method: 'post', url: "/Department/UserLogin" })
        .send({
            username: "18650018804",
            password: md5('123456'),
            device_id: md5(process.execPath + "-" + new Date().getTime() + "-" + Math.random())
        }).end(cb);
}
function getAliyunSignature(token, cb) {
    request({ token, url: "/Aliyun/Web" })
        .query({ type: 4 })
        .end(cb);
}


describe('/Aliyun test', function () {
    let companyId;
    let userId;
    let token;

    before('/Upload/login', function (done) {
        login((err, res) => {
            expect(err).to.eql(null);
            expect(res.body).to.be.an('object');
            expect(res.body.code).to.equal(0);
            companyId = res.body.data.company_id;
            userId = res.body.data.id;
            token = res.body.data.token;
            done();
        });
    })
    it('/Upload/upload file', function (done) {
        this.timeout(5 * 1000)
        getAliyunSignature(token, (err, res) => {
            expect(err).to.eql(null);
            expect(res.body).to.be.an('object');
            expect(res.body.code).to.equal(0);
            let data = res.body.data;
            superagent.put(data.host + "/" + data.dir + "txt/a.txt")
                .type('application/octet-stream')
                .send({
                    'key': data.dir + "txt/a.txt",
                    'policy': data.policy,
                    'OSSAccessKeyId': data.accessid,
                    'success_action_status': '200', //让服务端返回200,不然，默认会返回204
                    'signature': data.signature,
                })
                .attach('a.text', 'd:/a.txt')
                .end((err, res) => {
                    expect(err).to.eql(null);
                    console.log(err, res.status);
                    done();
                })
        })
    })
});